/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.supermercadobarateiro.bd;

import br.edu.ifal.supermercadobarateiro.modelo.EfetuarVenda;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Mikaely Anaeve
 */
public class BdEfetuarVenda {

    public void insere(EfetuarVenda ev) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();

        String sql = "insert into EfetuarVenda "
                + "(produto,quantidade,precounitario,precototal,codigoCliente) "
                + "values (?,?,?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, ev.getProduto());
        stmt.setInt(2, ev.getQuantidade());
        stmt.setInt(3, ev.getPrecoUnotario());
        stmt.setInt(4, ev.getPrecoTotal());
        stmt.setInt(5, ev.getCodigo().getCod());

        // stmt.setString(7, f.getSexo());


//        stmt.setDate(10, new java.sql.Date(f.getDatadeNascimento().getTimeInMillis()));
        stmt.execute();
        stmt.close();
        con.close();
    }

    public void altera(EfetuarVenda ev) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "update EfetuarVenda  set produto=?,quantidade=?,precoUnitario=?,precoTotal=?"
                + " where cod=?";

        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, ev.getProduto());
        stmt.setInt(2, ev.getQuantidade());
        stmt.setInt(3, ev.getPrecoUnotario());
        stmt.setInt(4, ev.getPrecoTotal());
        stmt.setInt(5, ev.getCod());

        stmt.execute();
        stmt.close();
        con.close();
    }

    public void remove(EfetuarVenda ev) throws SQLException {

        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = " delete from EfetuarVenda  where cod = ?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1, ev.getCod());
        stmt.execute();
        stmt.close();



    }

    public EfetuarVenda busca(int cod) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "select *from EfetuarVenda  where cod = ?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1, cod);
        ResultSet rs = stmt.executeQuery();
        EfetuarVenda ev1 = new EfetuarVenda();
        while (rs.next()) {

            ev1.setProduto(rs.getString("produto"));
            ev1.setPrecoUnotario(rs.getInt("precounitario"));
            ev1.setPrecoTotal(rs.getInt("precototal"));

            ev1.setQuantidade(rs.getInt("quantidade"));



        }
        rs.close();
        stmt.close();
        con.close();
        return ev1;

    }
}
